package com.sdry.service.impl.llm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.sdry.mapper.hpf.PositionMapper;
import com.sdry.mapper.llm.ClassesMapper;
import com.sdry.mapper.llm.EmployeeRecordMapper;
import com.sdry.mapper.llm.ProductionReportMapper;
import com.sdry.mapper.llm.WorkManageMapper;
import com.sdry.mapper.zc.ZcTestMapper;
import com.sdry.model.llm.Classes;
import com.sdry.model.llm.User;
import com.sdry.model.llm.Work;
import com.sdry.model.zc.ZcInspectionEntity;
import com.sdry.service.llm.WorkManageService;
import com.sdry.utils.DateUtil;

/**
 * 
 * @Title:WorkServiceImp
 * @Package com.sdry.service.impl.llm
 * @author llm
 * @date 2019年2月1日
 * @version 1.0
 */

@Service
public class WorkManageServiceImp implements WorkManageService {

	@Resource
	private EmployeeRecordMapper employeeRecordMapper;
	@Resource
	private PositionMapper positionMapper;
	@Resource
	private WorkManageMapper workManageMapper;
	@Resource
	private ClassesMapper classesMapper;
	@Resource
	private ProductionReportMapper productionReprotMapper;
	@Resource
	private ZcTestMapper zcTestMapper;
	
	/**       
	 * 作业记录查询
	 * @param positionName
	 * @param session
	 * @return    
	 * @throws ParseException 
	 */
	@Override
	public List<Work> getWork(String positionName, HttpSession session) throws ParseException {
		List<Work> list = new ArrayList<Work>();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(date);
		Integer year = Integer.parseInt(today.substring(0, 4));
		Integer mon = Integer.parseInt(today.substring(5, 7));
		//获取该月的所有日期
		List<String> timelist = DateUtil.getMonthFullDayByYearAndMonth(year, mon);
		List<ZcInspectionEntity> selectInspectionList = zcTestMapper.selectInspectionList();
		for(String dateDay : timelist){
			User user = (User)session.getAttribute("currentUser");
			Work work = new Work();
			work.setPositionName(positionName);
			
			//获取当前日期
			work.setWorkTime(dateDay);
			
			//当前的登录人信息
			work.setWorkName(user.getUserName());
			
			//查询当前工位的产量（根据员工ID和日期）
			Map<String, Object> map = new HashMap<>();
			map.put("uid", user.getUid());
			map.put("dateDay", dateDay);
			
			int number = 0;
			if(null != positionName && !"".equals(positionName)){
				switch(positionName){
				case "膜丝":
					number = productionReprotMapper.getCountMousseByTimeAndUid(map);
				break;
				case "组装":
					number = productionReprotMapper.getCountAssembleByTimeAndUid(map);
				break;
				case "离心":
					number = productionReprotMapper.getCountCentrifuleByTimeAndUid(map);
				break;
				case "拆端盖":
					number = productionReprotMapper.getCountSolidByTimeAndUid(map);
				break;
				case "固化":
					number = productionReprotMapper.getCountSolidByTimeAndUid(map);
				break;
				case "切割":
					number = productionReprotMapper.getCountCutByTimeAndUid(map);
				break;
				}
			}
			work.setOutput(number);
			
			//查询报检数量
			map.put("postionName", positionName);
			map.put("userName", user.getUserName());
			List<Classes> classesByWhite = classesMapper.getClassesByName("白班");
			Classes whiteClass = classesByWhite.get(0);
			String startTime = dateDay + " " + whiteClass.getStartTime();
			map.put("startTime", startTime);
			Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
			try {
				//设置时间为排产单时间
				ca.setTime(sdf.parse(dateDay + " 00:00:00"));
			} catch (ParseException e) {
				e.printStackTrace();
			} 
			//下一天
			ca.add(Calendar.DATE, +1);
			Date next = ca.getTime();
			String nextDay = sdf.format(next).substring(0, 10);
			String endTime = nextDay + " " + whiteClass.getStartTime();
			map.put("endTime", endTime);
			int i = 0;
//				ZcInspectionEntity w = workManageMapper.getInspectionNumber(map);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for(ZcInspectionEntity zc : selectInspectionList){
				if(zc.getiStation().equals(positionName) && zc.getiPerson().equals(user.getUserName()) && format.parse(startTime).getTime() < zc.getiDate().getTime() && zc.getiDate().getTime() < format.parse(endTime).getTime()){
					i += zc.getiNum();
				}
			}
			work.setInspectionNumber(i);
			list.add(work);
		}
		return list;
	}
	
	/**       
	 * 作业记录查询
	 */
	/*@Override
	public List<Work> getWork(String positionName, HttpSession session) {
		List<Work> list = new ArrayList<Work>();
		User user = (User)session.getAttribute("currentUser");
		Team team = (Team)session.getAttribute("team");
		Work work = new Work();
		work.setPositionName(positionName);
		String time = user.getRemark3();
		//获取当前日期
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = sdf.format(date);
		work.setWorkTime(today.substring(0, 10));
		
		//当前的登录人信息
		work.setWorkName(user.getUserName());
		
		//查询当前工位的产量（根据员工ID和日期）
		Map<String, Object> map = new HashMap<>();
		//Position position = positionMapper.getPositionByPname(positionName);
		List<Classes> classesByName = classesMapper.getClassesByName(team.getCrew());
		Classes cla = classesByName.get(0);
		List<Classes> classesByWhite = classesMapper.getClassesByName("白班");
		Classes whiteClass = classesByWhite.get(0);
		map.put("time", time);
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
		try {
			//设置时间为排产单时间
			ca.setTime(sdf.parse(time + " 00:00:00"));
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		//下一天
		ca.add(Calendar.DATE, +1);
		Date next = ca.getTime();
		String nextDay = sdf.format(next).substring(0, 10);
		
		//班次的开始时间
		Integer startTime = Integer.parseInt(cla.getStartTime().substring(0, 2));
		//班次的结束时间
		Integer endTime = Integer.parseInt(cla.getEndTime().substring(0, 2));
		//当前的小时
		Integer hourse = Integer.parseInt(today.substring(11, 13));
		//白班的开始时间
		Integer whiteStartTime = Integer.parseInt(whiteClass.getStartTime().substring(0, 2));
		//如果开始时间>结束时间，则是晚班
		if(startTime > endTime){
			//当前时间小于白班的开始时间，则是凌晨登录
			if(hourse < whiteStartTime){
				map.put("startTime", time + " " + cla.getStartTime());
				map.put("endTime", nextDay + " " + cla.getEndTime());
			//是晚上登录
			}else{
				map.put("startTime", time + " " + cla.getStartTime());
				map.put("endTime", time + " " + cla.getEndTime());
			}
		//如果是白班或夜班
		}else{
			//如果是夜班
			if(startTime < whiteStartTime){
				map.put("startTime", nextDay + " " + cla.getStartTime());
				map.put("endTime", nextDay + " " + cla.getEndTime());
			//如果是白班
			}else{
				map.put("startTime", time + " " + cla.getStartTime());
				map.put("endTime", time + " " + cla.getEndTime());
			}
		}
		map.put("uid", user.getUid());
		int number = 0;
		if(null != positionName && !"".equals(positionName)){
			switch(positionName){
			case "膜丝":
				number = workManageMapper.countMousseByUidAndTime(map);
			break;
			case "组装":
				number = workManageMapper.countAssembleByUidAndTime(map);
			break;
			case "离心":
				number = workManageMapper.countCentrifugleByUidAndTime(map);
			break;
			case "拆端盖":
				number = workManageMapper.countSolidByUidAndTime(map);
			break;
			case "固化":
				number = workManageMapper.countSolid2ByUidAndTime(map);
			break;
			case "切割":
				number = workManageMapper.countCutByUidAndTime(map);
			break;
			}
		}
		work.setOutput(number);
		//查询报检数量
		map.put("postionName", positionName);
		map.put("userName", user.getUserName());
		
		Integer inspectionNumber = workManageMapper.getInspectionNumber(map);
		System.out.println("第二次打印" + inspectionNumber);
		if("".equals(inspectionNumber) || null == inspectionNumber){
			inspectionNumber = 0;
		}
		work.setInspectionNumber(inspectionNumber);
		
		list.add(work);
		return list;
	}*/

	
}
